﻿namespace com.oakwoodsys.ConsultantCareDao
{
    using System;
    using System.Collections.Generic;
    using System.Linq;

    using com.oakwoodsys.SharePoint;

    using Microsoft.SharePoint;
    using Microsoft.SharePoint.Linq;

    using Context = ConsultantCareDataContext;

    using ReferenceObject = ConsultantActivityID;

    public class ConsultantActivityIDDao : BaseDao
    {
        #region Fields

        public const string ListName = "consultantactivities";

        #endregion Fields

        #region Constructors

        public ConsultantActivityIDDao( )
            : base( ListName )
        {
            CurrentWebUrl = SPContext.Current.Web.Url;
        }

        public ConsultantActivityIDDao( string currentWebUrl )
            : base( ListName )
        {
            CurrentWebUrl = currentWebUrl;
        }

        #endregion Constructors

        #region Methods

        public ReferenceObject Add( ReferenceObject value )
        {
            using ( Context c = new Context( CurrentWebUrl ) )
            {
                EntityList<ReferenceObject> l = c.GetList<ReferenceObject>( ListTitle );
                ReferenceObject v = clean( value );
                string title = v.Title;
                Guid g = Guid.NewGuid( );
                v.Title = g.ToString( );
                l.InsertOnSubmit( v );
                c.SubmitChanges( );
                v = ( from o in l
                      where o.Title == g.ToString( )
                      select o ).SingleOrDefault( );
                v.Title = title;
                c.SubmitChanges( );
                return v;
            }
        }

        public void Delete( int id )
        {
            using ( Context c = new Context( CurrentWebUrl ) )
            {
                EntityList<ReferenceObject> l = c.GetList<ReferenceObject>( ListTitle );
                var v = ( from o in l
                          where o.Id == id
                          select o );
                if ( v.Count( ) > 0 )
                {
                    l.DeleteAllOnSubmit( v );
                    c.SubmitChanges( );
                }
            }
        }

        public void Delete( Guid uniqueId )
        {
            using ( Context c = new Context( CurrentWebUrl ) )
            {
                EntityList<ReferenceObject> l = c.GetList<ReferenceObject>( ListTitle );
                var v = ( from o in l
                          where o.UniqueId == uniqueId
                          select o );
                if ( v.Count( ) > 0 )
                {
                    l.DeleteAllOnSubmit( v );
                    c.SubmitChanges( );
                }
            }
        }

        public void DeleteByActivity( Guid uniqueId )
        {
            using ( Context c = new Context( CurrentWebUrl ) )
            {
                EntityList<ReferenceObject> l = c.GetList<ReferenceObject>( ListTitle );
                var v = ( from o in l
                          where o.ActivityID == uniqueId.ToString( )
                          select o );
                if ( v.Count( ) > 0 )
                {
                    l.DeleteAllOnSubmit( v );
                    c.SubmitChanges( );
                }
            }
        }

        public void DeleteByCalendarEvent( Guid uniqueId )
        {
            using ( Context c = new Context( CurrentWebUrl ) )
            {
                EntityList<ReferenceObject> l = c.GetList<ReferenceObject>( ListTitle );
                var v = ( from o in l
                          where o.CalendarEventID == uniqueId.ToString( )
                          select o );
                if ( v.Count( ) > 0 )
                {
                    l.DeleteAllOnSubmit( v );
                    c.SubmitChanges( );
                }
            }
        }

        public void DeleteByConsultant( Guid uniqueId )
        {
            using ( Context c = new Context( CurrentWebUrl ) )
            {
                EntityList<ReferenceObject> l = c.GetList<ReferenceObject>( ListTitle );
                var v = ( from o in l
                          where o.ConsultantID == uniqueId.ToString( )
                          select o );
                if ( v.Count( ) > 0 )
                {
                    l.DeleteAllOnSubmit( v );
                    c.SubmitChanges( );
                }
            }
        }

        public ReferenceObject Get( int id )
        {
            using ( Context c = new Context( CurrentWebUrl ) )
            {
                EntityList<ReferenceObject> l = c.GetList<ReferenceObject>( ListTitle );
                ReferenceObject v = ( from o in l
                                      where o.Id.HasValue && o.Id.Value == id
                                      select o ).SingleOrDefault( );
                return v;
            }
        }

        public ReferenceObject Get( Guid id )
        {
            using ( Context c = new Context( CurrentWebUrl ) )
            {
                EntityList<ReferenceObject> l = c.GetList<ReferenceObject>( ListTitle );
                ReferenceObject v = ( from o in l
                                      where o.UniqueId == id
                                      select o ).SingleOrDefault( );
                return v;
            }
        }

        public List<ReferenceObject> GetAll( )
        {
            using ( Context c = new Context( CurrentWebUrl ) )
            {
                List<ReferenceObject> l = c.GetList<ReferenceObject>( ListTitle ).ToList( );
                return l;
            }
        }

        public List<ReferenceObject> GetByActivity( Guid uniqueId )
        {
            using ( Context c = new Context( CurrentWebUrl ) )
            {
                EntityList<ReferenceObject> l = c.GetList<ReferenceObject>( ListTitle );
                List<ReferenceObject> v = ( from o in l
                                            where o.ActivityID == uniqueId.ToString( )
                                            select o ).DefaultIfEmpty( ).ToList( );
                if ( v.Count == 1 && v[ 0 ] == null )
                    v.Clear( );
                return v;
            }
        }

        public List<ReferenceObject> GetByCalendarEvent( Guid uniqueId )
        {
            using ( Context c = new Context( CurrentWebUrl ) )
            {
                EntityList<ReferenceObject> l = c.GetList<ReferenceObject>( ListTitle );
                List<ReferenceObject> v = ( from o in l
                                            where o.CalendarEventID == uniqueId.ToString( )
                                            select o ).DefaultIfEmpty( ).ToList( );
                if ( v.Count == 1 && v[ 0 ] == null )
                    v.Clear( );
                return v;
            }
        }

        public List<ReferenceObject> GetByConsultant( Guid uniqueId )
        {
            using ( Context c = new Context( CurrentWebUrl ) )
            {
                EntityList<ReferenceObject> l = c.GetList<ReferenceObject>( ListTitle );
                List<ReferenceObject> v = ( from o in l
                                            where o.ConsultantID == uniqueId.ToString( )
                                            select o ).DefaultIfEmpty( ).ToList( );
                if ( v.Count == 1 && v[ 0 ] == null )
                    v.Clear( );
                return v;
            }
        }

        public bool DoesActivityExistForConsultant( Guid consultantId, Guid activityId )
        {
            using ( Context c = new Context( CurrentWebUrl ) )
            {
                EntityList<ReferenceObject> l = c.GetList<ReferenceObject>( ListTitle );
                List<ReferenceObject> v = ( from o in l
                                            where o.ConsultantID == consultantId.ToString( ) && 
                                            o.ActivityID == activityId.ToString( )
                                            select o ).DefaultIfEmpty( ).ToList( );
                if ( v.Count == 1 && v[ 0 ] == null )
                    v.Clear( );
                return v.Count > 0;
            }
        }

        public List<ReferenceObject> GetByRecruiter( Guid uniqueId )
        {
            using ( Context c = new Context( CurrentWebUrl ) )
            {
                EntityList<ReferenceObject> l = c.GetList<ReferenceObject>( ListTitle );
                List<ReferenceObject> v = ( from o in l
                                            where o.RecruiterID == uniqueId.ToString( )
                                            select o ).DefaultIfEmpty( ).ToList( );
                if ( v.Count == 1 && v[ 0 ] == null )
                    v.Clear( );
                return v;
            }
        }

        public List<Consultant> GetConsultantsByRecruiter( Guid uniqueId )
        {
            List<Consultant> consultants = new List<Consultant>( );
            List<ReferenceObject> list = GetByRecruiter( uniqueId );
            ConsultantDao dao = new ConsultantDao( CurrentWebUrl );
            foreach ( ReferenceObject o in list )
            {
                Guid id = new Guid( o.ConsultantID );
                Consultant consultant = dao.Get( id );
                if ( consultant != null )
                    consultants.Add( consultant );
            }
            return consultants;
        }

        public bool ReferencesExistForRecruiter( Guid uniqueId )
        {
            using ( Context c = new Context( CurrentWebUrl ) )
            {
                EntityList<ReferenceObject> l = c.GetList<ReferenceObject>( ListTitle );
                List<ReferenceObject> v = ( from o in l
                                            where o.RecruiterID == uniqueId.ToString( )
                                            select o ).DefaultIfEmpty( ).ToList( );
                if ( v.Count == 1 && v[ 0 ] == null )
                    v.Clear( );
                return v.Count > 0;
            }
        }

        ReferenceObject clean( ReferenceObject value )
        {
            string title = value.Title;
            if ( title.EndsWith( ".aspx" ) )
            {
                int i = title.LastIndexOf( '/' );
                title = title.Substring( 0, i );
            }
            return new ReferenceObject
            {
                ActivityID = value.ActivityID,
                CalendarEventID = value.CalendarEventID,
                ConsultantID = value.ConsultantID,
                RecruiterID = value.RecruiterID,
                Title = title
            };
        }

        #endregion Methods
    }
}